﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.View.Proxy;
using EFong.View.Customer;

namespace EFong.View.IntergralExchange
{
    public partial class Frm_IntergralExchangeEdit : BasicForm
    {
        private Proxy.BaseServiceProxy _baseProxy {get{ return Proxy.BaseServiceProxy.GetInstance();}}
        private Proxy.MasterServiceProxy _masterProxy {get{ return Proxy.MasterServiceProxy.GetInstance();}}
        private Proxy.BusinessServiceProxy _businessProxy {get{ return Proxy.BusinessServiceProxy.GetInstance();}}

        public Frm_IntergralExchangeEdit()
        {
            InitializeComponent();
            spe_LeftIntergral.SetValue(null);
        }

        protected override void InitFields()
        {
        }

        protected override void Events()
        {
            this.cmb_Customer.EditValueChanged += (s, e) =>
                {
                    Contract.RCustomer customer = this.cmb_Customer.GetValue() as Contract.RCustomer;

                    Customer = _masterProxy.GetCustomerByID(customer.ID).ReturnValue;
                };

            //this.spe_Qty.Validated += (s, e) => { updateTotalFields(); };
            this.spe_ExchangeIntergral.Validated += (s, e) => { updateTotalFields(); };

            this.btn_CustomerData.Click += (s, e) =>
                {
                    browserCustomerData();
                };
        }

        protected override void LoadDatas()
        {
            //this.cmb_Customer.Properties.BindingData(_masterProxy.GetAllCustomer().ReturnValue);
            bds_Customer.BindingData(_masterProxy.GetAllCustomer().ReturnValue);
        }

        private void browserCustomerData()
        {
            if (Customer == null)
                return;

            Frm_CustomerEdit frm = new Frm_CustomerEdit();
            frm.Text = "客户详细资料";
            frm.ViewState = ViewStates.BROWSER;
            frm.Load += (s, e) =>
            {
                frm.CurCust = Customer;
            };
            FormOpener.ShowDialog(frm);
        }

        private void updateTotalFields()
        {
            Contract.IntergralExchange detail = getDetail();

            if (detail.Customer == null)
                this.spe_LeftIntergral.SetValue(null);
            else
                this.spe_LeftIntergral.SetValue(detail.Customer.Intergral - detail.Amount);
        }

        private void bar_Main_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "bbt_Save":
                    this.save();
                    break;
                case "bbt_Close":
                    this.Close();
                    break;
                default:
                    break;
            }
        }

        private void save()
        {
            Contract.IntergralExchange detail = null;

            UIOperManager.SaveData(
                () =>
                {
                    return true;
                },
                () =>
                {
                    detail = getDetail();
                    updateToCurDetail(detail);
                    return _businessProxy.IntergralExchange(CurDetail).IsSuccess;
                },
                string.Empty,
                () =>
                {
                    DialogResult = DialogResult.OK;
                    this.Close();
                });
        }

        private Contract.IntergralExchange _curDetail;
        public Contract.IntergralExchange CurDetail
        {
            get { return _curDetail; }
            set
            {
                _curDetail = value;
                onCurDetailChanged();
            }
        }

        private void onCurDetailChanged()
        {
            if (CurDetail == null)
            {

            }
            else
            {
                this.cmb_Customer.SetValue(CurDetail.Customer);

                this.txt_ContactPerson.SetText(CurDetail.Receiptor);
                this.txt_Item.SetText(CurDetail.Item);
                this.spe_Qty.SetValue(CurDetail.Qty);
                this.dte_ExchangeDate.SetValue(CurDetail.Date);
                this.spe_ExchangeIntergral.SetValue(CurDetail.Amount);

                updateTotalFields();
            }
        }

        private Contract.IntergralExchange getDetail()
        {
            Contract.IntergralExchange detail = new Contract.IntergralExchange();

            detail.Customer = Customer;

            detail.Date = this.dte_ExchangeDate.GetValue().Value;
            detail.Amount = this.spe_ExchangeIntergral.GetValue();
            detail.Qty = (int)this.spe_Qty.GetValue();
            detail.Item = this.txt_Item.GetText();
            detail.Receiptor = this.txt_ContactPerson.GetText();
            detail.Remark = this.txt_Remark.GetText();

            return detail;
        }

        private void updateToCurDetail(Contract.IntergralExchange detail)
        {
            CurDetail.Amount = detail.Amount;
            CurDetail.Customer = detail.Customer;
            CurDetail.Date = detail.Date;
            CurDetail.Item = detail.Item;
            CurDetail.Qty = detail.Qty;
            CurDetail.Receiptor = detail.Receiptor;
            CurDetail.Remark = detail.Remark;
        }



        private Contract.Customer _customer;
        public Contract.Customer Customer
        {
            get { return _customer; }
            set
            {
                _customer = value;
                onCustomerChanged();
            }
        }

        private void onCustomerChanged()
        {
            if (Customer == null)
            {
                ControlExtensions.ClearValue(
                    this.spe_CustomerIntergral
                    );
            }
            else
            {
                this.spe_CustomerIntergral.SetValue(Customer.Intergral);
            }

            updateTotalFields();
        }
    }
}
